import json
import time

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def login():
    driver = webdriver.Chrome()
    driver.get('https://docs.qq.com/')
    driver.find_element(By.XPATH,'//div//button[2]//div').click()
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//div[@class='compliance-label']"))
    )
    # driver.find_element(By.XPATH,"//div[@class='compliance-label']").click()
    element.click()
    print('已经点击')
    print(element)
    input("请手动完成登录和验证，然后按回车继续...")
    # driver.find_element(By.XPATH,'//div//button[@class="weui-btn weui-btn_primary js_quick_login_btn qlogin_btn"]').click()
    # try:
    #     element1 = WebDriverWait(driver, 10).until(
    #     EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/button'))
    #         )
    #     element1.click()
    # except TimeoutException:
    #     print('没有找到元素')


    cookies = driver.get_cookies()
    with open("cookies.json", "w") as f:
        json.dump(cookies, f)
    driver.quit()
def second_login():
    # 初始化浏览器
    driver = webdriver.Chrome()
    # 访问目标网站
    driver.get("https://docs.qq.com/")
    with open("cookies.json", "r") as f:
        cookies = json.load(f)
        for cookie in cookies:
            driver.add_cookie(cookie)
        driver.refresh()
    time.sleep(5)
if __name__ == '__main__':
    second_login()